/**
 * 
 */
package cn.leaves.reader;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * @author leaves
 * 
 */
public class PageView extends SurfaceView implements SurfaceHolder.Callback {
	private SurfaceHolder mSurfaceHolder;
	private PageViewThread thread = null;
	private boolean threadStarted = false;

	/* jni method */
	/* These should be native functions */
	protected static native int mupdfOpenFile(String filename);

	protected static native void mupdfGotoPage(int localActionPageNum);

	protected static native float mupdfPageWidth();

	protected static native float mupdfPageHeight();

	protected static native void mupdfDrawPage(Bitmap bitmap, int pageW, int pageH, int patchX, int patchY, int patchW,
			int patchH);

	public PageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public PageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public PageView(Context context) {
		super(context);
		initializePageView();
	}

	private void initializePageView() {
		mSurfaceHolder = getHolder();
		mSurfaceHolder.addCallback(this);
		thread = new PageViewThread(mSurfaceHolder);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		// if (thread.onTouchEvent(event))
		// return true;
		return super.onTouchEvent(event);
	}

	/* SurfaceHolder.Callback implement method */

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		thread.start();

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		thread.destroy();
	}

}
